package com.nami.store.dubbo;

import cn.hutool.core.util.ObjectUtil;
import com.nami.store.api.request.user.LoginReq;
import com.nami.store.api.request.user.UserAddRequest;
import com.nami.store.api.service.RemoteUserService;
import com.nami.store.api.response.user.UserDTO;
import com.nami.store.biz.user.application.UserAppService;
import com.nami.store.biz.user.convert.UserConvert;
import com.nami.store.domain.UserDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.json.utils.JsonUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @Author: lbc
 * @Date: 2023-07-24 15:50
 * @email: 594599620@qq.com
 * @Description: keep coding
 */
@Slf4j
@DubboService
@RequiredArgsConstructor
public class RemoteUserServiceImpl implements RemoteUserService {

    private final UserAppService userAppService;

    @Override
    public UserDTO getUserByUserId(String appKey, String userId) {
        return userAppService.getUserById(appKey, userId);
    }

    @Override
    public List<UserDTO> getUserByUserIds(List<String> userIds) {
        return userAppService.getUserByIds(userIds);
    }

    @Override
    public UserDTO getUserByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        UserDTO user = userAppService.getUserByUserId(userId);
        log.info("[nami-user] get user:{}", JsonUtils.toJsonString(user));
        return user;
    }

    @Override
    public Map<String, UserDTO> getUsersByUserIds(List<String> userIds) {
        return userAppService.getUsersByIds(userIds);
    }

    @Override
    public UserDTO getUserByMobile(String mobile) {
        return userAppService.getUserByMobile(mobile);
    }

    @Override
    public UserDTO getUserByAccount(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        UserDO user = userAppService.getUserByAccount(account);
        if (ObjectUtil.isNull(user)) {
            return null;
        }
        return UserConvert.INSTANCE.convert(user);
    }

    @Override
    public boolean save(UserAddRequest request) {
        return userAppService.saveUser(request);
    }

    @Override
    public boolean batchSave(List<UserAddRequest> list) {
       return userAppService.batchSave(list);
    }

    @Override
    public boolean updateLoginInfoByUserId(LoginReq request) {
        log.info("[user-logininfo] 更新登录信息 dto：{}", ToStringBuilder.reflectionToString(request, ToStringStyle.SHORT_PREFIX_STYLE));
        if (ObjectUtil.isNull(request) || request.getUserId() == null) {
            return Boolean.FALSE;
        }

        UserDO user = new UserDO();
        user.setId(request.getUserId());
        user.setLastLoginIp(request.getLoginIp());
        user.setLastLoginTime(request.getLoginTime());
        user.setUpdateTime(new Date());

        // to do

        return Boolean.TRUE;
    }


}
